package com.twu.biblioteca;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: aulme
 * Date: 05/07/2013
 * Time: 16:48
 * To change this template use File | Settings | File Templates.
 */
public class HardcodedUserRepository implements UserRepository {

    private final Map<String, User> usersIndexedByLibraryNumber;
    private User authenticatedUser;

    public HardcodedUserRepository(Map<String, User> usersIndexedByLibraryNumber) {
        this.usersIndexedByLibraryNumber = usersIndexedByLibraryNumber;
    }

    @Override
    public boolean areCredentialsCorrect(String libraryNumber, String password) {
        if (!this.usersIndexedByLibraryNumber.containsKey(libraryNumber)) {
            return false;
        }

        return this.usersIndexedByLibraryNumber.get(libraryNumber).isPasswordCorrect(password);
    }

    @Override
    public boolean isAuthenticated() {
        return this.authenticatedUser != null;
    }

    @Override
    public boolean authenticateUser(String userLibraryNumber, String password) {
        if (! this.areCredentialsCorrect(userLibraryNumber, password)) {
            return false;
        }

        this.authenticatedUser = this.usersIndexedByLibraryNumber.get(userLibraryNumber);
        return true;
    }

    @Override
    public User getAuthenticatedUser() {
        return this.authenticatedUser;
    }
}
